package com.car.web.controller;

import com.car.pojo.Menu;
import com.car.pojo.SysUser;
import com.car.service.SysMenuService;
import com.car.service.SysUserMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Controller
public class MenuController {
    @Autowired
    private SysMenuService menuService;

    @GetMapping(value = "/list_v3")
    public String list(Model model, Menu menu, @RequestParam(defaultValue = "1") Integer pageNo) {
        Integer pageSize = 3;

        List<Menu> list = menuService.list(menu, pageSize, pageNo);
        model.addAttribute("list", list);
        Integer totalCount = menuService.selectSysUserCount(menu);
        Integer totalPages = totalCount / pageSize;
        if (totalCount % pageSize != 0) {
            totalPages++;
        }
        model.addAttribute("list", list);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        return "index_v3";
    }

    @PostMapping("/add_v3")
    public String add(Menu menu) {
        int result = menuService.add(menu);
        if (result > 0) {
            return "redirect:list_v3";
        }
        return "add_v3";
    }

    @GetMapping("/delete_v3")
    public String delete(@RequestParam Integer menu_id) {
        int result = menuService.delete( menu_id);
        if (result > 0) {
            return "redirect:list_v3";
        }
        return "delete_v3";
    }

    @PostMapping("/toEdit_v3")
    public String toEdit(Menu menu) {
        int result = menuService.update(menu);
        if (result > 0) {
            return "redirect:list_v3";
        }
        return "toEdit_v3";
    }

    @GetMapping("/edit_v3")
    public String edit(Model model, Integer  menu_id) {
        Menu menu = menuService.getByid(menu_id);
        model.addAttribute("menu", menu);

        return "edit_v3";
    }
}
